package com.lilei.serialize;

import javax.xml.bind.annotation.XmlRootElement;
import java.io.*;

/**
 * @Desc
 * @Auther 李雷(KyLin)
 * @Date 2020/03/31
 */
@XmlRootElement //此注解是javaXML需要使用
public class User implements Serializable {
    private static final long serialVersionUID = 2221260523307063588L;
    private transient String name;

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getParentAge() {
        return parentAge;
    }

    public void setParentAge(Integer parentAge) {
        this.parentAge = parentAge;
    }

    private Integer age;
    private Integer parentAge;

    public User(String name, Integer age, Integer parentAge) {
        this.name = name;
        this.age = age;
        this.parentAge = parentAge;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", parentAge=" + parentAge +
                '}';
    }
    // 看dubbo 支持什么序列化
    // https://github.com/apache/dubbo  #  dubbo-serialization  这个项目可以看到

    private void writeObject(ObjectOutputStream out) {

        // 看arraylist里面会发现用了transient 优化，保证数据是最小化传输

        // 有序写入字段，按照顺序
        try {
            out.defaultWriteObject();

            out.writeObject(name);
            out.writeObject(age);
            out.writeObject(parentAge);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void readObject(ObjectInputStream in) {

        try {
            in.defaultReadObject();
            // 有序读取字段
            name = (String) in.readObject();
            age = (Integer) in.readObject();
            parentAge = (Integer) in.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}
